package com.wondertek.poms.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * <p><b>FileIoUtiles Description:</b> 文件操作类</p>
 * @author douzi
 * <b>DATE</b> 2020年12月23日 下午3:54:21
 */
@Slf4j
public class FileIoUtiles {

    public static void fileCopy(String src, String tar, boolean isDelete) {
        File srcFile = new File(src);
        File tarFile = new File(tar);

        if (srcFile.isDirectory()) {
            return;
        }
        if ((isDelete) && (tarFile.exists()))
            tarFile.delete();
        else if ((!(isDelete)) && (tarFile.exists()))
            return;
        tarFile.getParentFile().mkdirs();
        FileInputStream is = null;
        FileOutputStream os = null;
        try {
            is = new FileInputStream(srcFile);
            os = new FileOutputStream(tarFile);

            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = is.read(buffer, 0, 8192)) != -1)
                os.write(buffer, 0, bytesRead);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ea) {
            ea.printStackTrace();
        } catch (Exception en) {
            en.printStackTrace();
        } finally {
            try {
                if (is != null)
                    is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (os != null)
                    os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void deleteFile(String tmpPath) {
        File file = new File(tmpPath);
        if (file.exists()) {
            file.delete();
        }
    }

    public static int deleteFile(File file) {
        if (file == null) {
            return -1;
        } else if (file.exists()) {
            if (file.isFile()) {
                file.delete();
            }

            if (file.isDirectory()) {
                File[] subFiles = file.listFiles();
                if (subFiles != null) {
                    for(int i = 0; i < subFiles.length; ++i) {
                        deleteFile(subFiles[i]);
                    }
                }

                file.delete();
            }

            return 0;
        } else {
            return -1;
        }
    }

    /**
     * 将字符串content的内容追加到文件filePath的最后，如果文件不存在，先创建新文件
     * 追加文件：在构造FileOutputStream时，把第二个参数设为true 编码：UTF-8
     *
     * @param filePath
     *            文件路径+文件名
     * @param content
     *            内容
     */
    public static boolean writeFileAppend(String filePath, String content) {
        boolean res = true;
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filePath, true), "UTF-8"));
            out.write(content);
        } catch (Exception e) {
            log.error(e.getMessage());
            res = false;
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
                log.info(e.toString());
            }
        }
        return res;
    }


}
